using FytSoa.Application.Cm.Param;
using FytSoa.Domain.Cm;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Cm;

/// <summary>
/// 社区评论服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v6")]
public class CmCommentService : IApplicationService 
{
    private readonly SugarRepository<CmComment> _thisRepository;
    private readonly SugarRepository<CmCommunity> _communityRepository;
    public CmCommentService(SugarRepository<CmComment> thisRepository
    ,SugarRepository<CmCommunity> communityRepository)
    {
        _thisRepository = thisRepository;
        _communityRepository = communityRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CmCommentDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Status=="1",m=>m.Audit)
            .WhereIF(param.Id!=0,m=>m.CommunityId==param.Id)
            .Includes(m=>m.Community)
            .Includes(m=>m.User)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<CmCommentDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CmCommentDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<CmCommentDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task<bool> AddAsync(CmCommentAddParam model)
    {
        await _communityRepository.UpdateAsync(m=>new CmCommunity()
        {
            Comment = m.Comment+1
        },m=>m.Id==model.CommunityId);
        return await _thisRepository.InsertAsync(model.Adapt<CmComment>());
    }

    /// <summary>
    /// 添加评论回复
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task<bool> AddReplyAsync(CommentReplyParam model)
    {
        var entity = await _thisRepository.GetFirstAsync(m => m.Id == model.Id);
        entity.ReplyBody.Add(new CmComment.CmCommentReply()
        {
            UserId = model.UserId,
            NickName = model.NickName,
            Avatar=model.Avatar,
            Content = model.Content,
            ByUserId = model.ByUserId,
            ByNickName = model.ByNickName
        });
        await _communityRepository.UpdateAsync(m=>new CmCommunity()
        {
            Comment = m.Comment+1
        },m=>m.Id==entity.CommunityId);
        return await _thisRepository.UpdateAsync(entity);
    }
        

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CmCommentAddParam model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CmComment>());
    
    /// <summary>
    /// 审核
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAuditAsync(CmCommunityAuditParam param)=> 
        await _thisRepository.UpdateAsync(m => new CmComment()
        {
            Audit = param.Audit
        },m=>param.Ids.Contains(m.Id));

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
